/* xx13.5cim.p - Gen CIM for BOM Import (from eb2.1 to 2015EE)
   Created By: Hayden (2015EE) 2016-06-13
   
   Purpose: Generate CIM for 2015EE Bom Import base on eb2.1
   Runtime: eb2.1
   Cimload: 2015EE
   
   Upgrade Step:
     1. run program in eb2.1 generate *.cim files
     2. ftp *.cim files to 2015ee
     3. doing cimload in each domain by cim file prefix
*/

{mfdtitle.i}
def var DOMAIN_PRC as char init 'PRC'.
def var DOMAIN_HK  as char init 'HK'.

def temp-table tpt_mstr
  field tpt_part like pt_part
  index itpt_mstr is primary tpt_part.
  
def temp-table tps_mstr
  field tps_par      like ps_par
  field tps_comp     like ps_comp
  field tps_ref      like ps_ref
  field tps_start    like ps_start
  field tps_qty_per  like ps_qty_per
  field tps_ps_code  like ps_ps_code
  field tps_end      like ps_end
  field tps_rmks     like ps_rmks
  field tps_scrp_pct like ps_scrp_pct
  field tps_lt_off   like ps_lt_off
  field tps_op       like ps_op
  field tps_item_no  like ps_item_no
  field tps_fcst_pct like ps_fcst_pct
  field tps_group    like ps_group
  field tps_process  like ps_process
  index itps_mstr is primary tps_par tps_comp tps_ref tps_start.


/* empty temp-table tpt_mstr */
function emptypt returns logical ():
  for each tpt_mstr exclusive-lock:
    delete tpt_mstr.
  end.
  return yes.
end.

/* write out temp-table tps_mstr to file */
function writeps returns logical (input file as char):
  def var i as int.
  def var j as int initial 1.
  def var f as char.
  
  def var prefix as char.
  def var suffix as char.
  
  prefix = substring(file, 1, r-index(file, "*") - 1).
  suffix = substring(file, r-index(file, "*") + 1).
  f = prefix + string(j) + suffix.
  
  output to value(f).
  for each tps_mstr no-lock:
    i = i + 1.
    if i = 30001 then do:
      i = 1.
      output close.
      j = j + 1.
      f = prefix + string(j) + suffix.
      os-delete value(f).
      output to value(f) append.
    end.
    if i = 1 then do:
      put "ps_par|ps_comp|ps_ref|ps_start|"
          "ps_qty_per|ps_ps_code|ps_start|"
          "ps_end|ps_rmks|ps_scrp_pct|ps_lt_off|"
          "ps_op|ps_item_no|ps_fcst_pct|ps_group|"
          "ps_process|" skip.
    end.
    
    def var v_start as char.
    def var v_end as char.
    
    if tps_start = ? then v_start = ''.
    else v_start = string(tps_start).
    
    if tps_end = ? then v_end = ''.
    else v_end = string(tps_end).
    
    put    tps_par
      "|"  tps_comp
      "|"  tps_ref
      "|'" v_start
      "|"  tps_qty_per
      "|"  tps_ps_code
      "|'" v_start
      "|'" v_end
      "|'" tps_rmks
      "|"  decimal(tps_scrp_pct)
      "|"  int(tps_lt_off)
      "|"  int(tps_op)
      "|"  int(tps_item_no)
      "|"  decimal(tps_fcst_pct)
      "|"  tps_group
      "|"  tps_process
      "|"  skip.
  end.
  output close.
  return yes.
end.

/* check if bom record has cached in tps_mstr */
function isexists returns logical (
    input par   as char,
    input comp  as char,
    input ref   as char,
    input start as date
    ):
  find first tps_mstr where tps_par = par
                        and tps_comp = comp
                        and tps_ref = ref
                        and tps_start = start
                        no-lock no-error.
  return available(tps_mstr).
end.

/* load bom tree into tps_mstr */
function getbom returns logical (input rpart as char):
  for each ps_mstr where ps_domain = global_domain
                     and ps_par = rpart
                     and ps_comp <> ""
                     and (ps_end = ? or ps_end > today)
                     no-lock:
    if not isexists(ps_par, ps_comp, trim(ps_ref), ps_start) then do:
      create tps_mstr.
      assign tps_par      = ps_par
             tps_comp     = ps_comp
             tps_ref      = trim(ps_ref)
             tps_start    = ps_start
             tps_qty_per  = ps_qty_per
             tps_ps_code  = ps_ps_code
             tps_end      = ps_end
             tps_rmks     = ps_rmks
             tps_scrp_pct = ps_scrp_pct
             tps_lt_off   = ps_lt_off
             tps_op       = ps_op
             tps_item_no  = ps_item_no
             tps_fcst_pct = ps_fcst_pct
             tps_group    = ps_group
             tps_process  = ps_process.
      release tps_mstr.
      getbom(ps_comp).
    end.
  end.
  return yes.
end.

/* load active active parts into tpt_mstr */
function readpt returns logical (input file as char):
  emptypt().
  input from value(file).
  def var part like pt_part.
  def var i as int init 0.
  repeat:
    import delimiter "|" part.
    if i > 0 then do:
      create tpt_mstr.
      assign tpt_part = part.
      release tpt_mstr.
    end.
    i = i + 1.
  end.
  return yes.
end.

/* get all P/N from tps_mstr and import into tpt_mstr */
function uniquept returns logical ():
  emptypt().
  for each tps_mstr no-lock:
    find first tpt_mstr where tpt_part = tps_par no-lock no-error.
    if not available(tpt_mstr) then do:
      create tpt_mstr.
      assign tpt_part = tps_par.
      release tpt_mstr.
    end.
    
    find first tpt_mstr where tpt_part = tps_comp no-lock no-error.
    if not available(tpt_mstr) then do:
      create tpt_mstr.
      assign tpt_part = tps_comp.
      release tpt_mstr.
    end.
  end.
  return yes.
end.

/* write out tpt_mstr to file */
function writept returns logical (input file as char):
  os-delete value(file).
  output to value(file).
  for each tpt_mstr no-lock:
    put unformatted tpt_part skip.
  end.
  output close.
  return yes.
end.

/* gen cim for 2015ee 13.5 Product Structure */
function genpscim return logical (input file as char):
  os-delete value(file).
  output to value(file).
  for each tps_mstr no-lock:
    def var v_start as char.
    def var v_end as char.
    
    if tps_start = ? then v_start = ''.
    else v_start = string(tps_start).
    
    if tps_end = ? then v_end = ''.
    else v_end = string(tps_end).
    
    put '@@batchload bmpsmt.p' skip.
    put unformatted '"' tps_par '"' skip.
    put unformatted '"' tps_comp '" "' tps_ref '" "' v_start '"' skip.
    put unformatted tps_qty_per ' "' tps_ps_code '" "' v_start '" "' v_end '" "' tps_rmks '" ' tps_scrp_pct ' ' tps_lt_off ' ' tps_op ' ' tps_item_no ' ' tps_fcst_pct ' "' tps_group '" "' tps_process '"' skip.
    put '@@end' skip.
  end.
  output close.
  return yes.
end.

/*----------------*/
def var infile as char format 'x(16)' init 'active-fgs.txt' label 'FGS File'.

def var cimname as char format 'x(12)' init '13.5-bom' label 'CIM File'.
def var cimfile_prc as char format 'x(20)'.
def var cimfile_hk as char format 'x(20)'.

def var pnsname as char format 'x(12)' init 'active-pns' label 'P/N File'.
def var pnsfile as char format 'x(20)'.

def var outname as char format 'x(12)' init '13.5' label 'Out File'.
def var outfile as char format 'x(20)'.

cimfile_prc = DOMAIN_PRC + '-' + cimname + '.cim'.
cimfile_hk  = DOMAIN_HK  + '-' + cimname + '.cim'.
pnsfile     =                    pnsname + '.txt'.
outfile     =                    outname + '-*.txt'.

repeat:
  disp 'Generate CIM for 2015EE Bom Import base on eb2.1' skip(1)
       'Upgrade Steps:' skip
       '  1. run program in eb2.1 generate *.cim files' skip
       '  2. ftp *.cim files to 2015ee' skip
       '  3. doing cimload in each domain by cim file prefix' skip(1)
       infile  colon 14 skip
       cimname colon 14 '.cim' space(2) cimfile_prc no-label view-as text
                               space(2) cimfile_hk  no-label view-as text skip
       pnsname colon 14 '.txt' space(2) pnsfile     no-label view-as text skip
       outname colon 14 '.txt' space(2) outfile     no-label view-as text skip
  with frame a width 80 side-label.

  prompt-for infile cimname pnsname outname with frame a editing:
    if frame-field = 'cimname' then do:
      if input cimname <> "" then do:
        disp DOMAIN_PRC + '-' + input cimname + '.cim' @ cimfile_prc with frame a.
        disp DOMAIN_HK  + '-' + input cimname + '.cim' @ cimfile_hk  with frame a.
      end.
      else do:
        disp '' @ cimfile_prc with frame a.
        disp '' @ cimfile_hk  with frame a.
      end.
    end.
      
    if frame-field = 'pnsname' then do:
      if input pnsname <> "" then do:
        disp input pnsname + '.txt' @ pnsfile with frame a.
      end.
      else do:
        disp '' @ pnsfile with frame a.
      end.
    end.

    if frame-field = 'outname' then do:
      if input outname <> "" then do:
        disp input outname + '-*.txt' @ outfile with frame a.
      end.
      else do:
        disp '' @ outfile with frame a.
      end.
    end.
    
    status input.
    readkey.
    apply lastkey.
  end.
  
  infile  = input infile .
  cimname = input cimname.
  pnsname = input pnsname.
  outname = input outname.
  
  if infile = "" or cimname = "" or pnsname = "" or outname = "" then do:
    message 'Error: Please enter all filenames'.
    next.
  end.
  
  cimfile_prc = DOMAIN_PRC + '-' + cimname + '.cim'.
  cimfile_hk  = DOMAIN_HK  + '-' + cimname + '.cim'.
  pnsfile     =                    pnsname + '.txt'.
  outfile     =                    outname + '-*.txt'.  
  
  /* load all fgs part from active-fgs.txt */
  if readpt(infile) <> yes then do:
    message 'Error: file ' + infile + ' not exists'.
    next.
  end.
  
  /* load all fgs bom product structure */
  for each tpt_mstr no-lock:
    getbom(tpt_part).
  end.
  
  /* generate cim file by all product structure */
  genpscim(cimfile_prc).
  
  /* copy cimfile_prc as cimfile_hk */
  os-copy value(cimfile_prc) value(cimfile_hk).
  
  /* write out all fgs product structure to file */
  writeps(outfile).
  
  /* reload all P/N from fgs product structure to tpt_mstr */
  uniquept().
  
  /* write out all P/N to a file list */
  writept(pnsfile).
  
  message 'Completed!'.
end.
